<template>
  <div class="login-root">
    <div class="container" :class="{ 'right-panel-active': isSignUp }">
      <!-- 注册表单 -->
      <div class="form-container sign-up-container">
        <form>
          <h2 class="title">注册</h2>
          <input type="text" placeholder="账户" class="input" />
          <input type="email" placeholder="邮箱" class="input" />
          <input type="password" placeholder="密码" class="input" />
          <button class="btn">注册</button>
        </form>
      </div>
      <!-- 登录表单 -->
      <div class="form-container sign-in-container">
        <form :model="user" @submit.prevent>
          <h2 class="title">登录</h2>
          <input type="text" placeholder="账户" class="input" v-model="user.username" />
          <input type="password" placeholder="密码" class="input" v-model="user.password" />
          <a href="#" class="link">忘记密码?请点击</a>
          <button class="btn" @click="loginHandle">登录</button>
        </form>
      </div>
      <!-- overlay -->
      <div class="overlay-container">
        <div class="overlay">
          <div class="overlay-panel overlay-left">
            <div class="overlay-content">
              <h2>欢迎登录</h2>
              <p>请完善您的个人信息</p>
              <button class="btn ghost" @click="isSignUp = false">回去登录</button>
            </div>
          </div>
          <div class="overlay-panel overlay-right">
            <div class="overlay-content">
              <h2>欢迎登录隧道管理系统</h2>
              <p>没有账号？去注册</p>
              <button class="btn ghost" @click="isSignUp = true">注册</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import api from '@/api/index.js'
import {useLoginStore} from '@/stores/loginStore'
import {useRouter} from 'vue-router'

const isSignUp = ref(false)

// 声明用户信息
const user = reactive({
  username: '',
  password: ''
})

const loginStore = useLoginStore()

// 获取路由对象
const router = useRouter()
/**
 * 登录事件
 */
const loginHandle = () => {
  console.log(user);
  api.getLogin({
    username: user.username,
    password: user.password
  }).then(res => {
    console.log(res)
    if (res.data.status === 200){
      console.log('登录成功');

      loginStore.setLoginInfo(res.data.token,res.data.permission,res.data.username)

      ElMessage({
        message: '登录成功',
        type: 'success',
        plain: true,
      })

      router.push('/')
    } else {
      console.log('登录失败');
      ElMessage({
        message: res.data.msg,
        type: 'error',
        plain: true,
      })
    }
  })
}

</script>

<style scoped>
.login-root {
  width: 100vw;
  height: 100vh;
  background: url('@/assets/images/background_login.jpg') no-repeat center center/cover;
  display: flex;
  align-items: center;
  justify-content: center;
}

.container {
  border-radius: 12px;
  box-shadow: 0 0.9rem 1.7rem rgba(0, 0, 0, 0.25), 0 0.7rem 0.7rem rgba(0, 0, 0, 0.22);
  position: relative;
  overflow: hidden;
  width: 900px;
  max-width: 100%;
  min-height: 500px;
  display: flex;
}

.form-container {
  position: absolute;
  top: 0;
  height: 100%;
  transition: all 0.6s ease-in-out;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 50%;
  background: #fff;
  z-index: 2;
  box-sizing: border-box;
  padding: 0 80px;
}

.sign-in-container {
  left: 0;
  opacity: 1;
  z-index: 2;
}
.sign-up-container {
  left: 0;
  opacity: 0;
  z-index: 1;
}

.container.right-panel-active .sign-in-container {
  transform: translateX(100%);
  opacity: 0;
  z-index: 1;
}
.container.right-panel-active .sign-up-container {
  transform: translateX(100%);
  opacity: 1;
  z-index: 2;
}

.overlay-container {
  position: absolute;
  top: 0;
  left: 50%;
  width: 50%;
  height: 100%;
  overflow: hidden;
  z-index: 100;
  transition: transform 0.6s ease-in-out;
}

.overlay {
  background: rgba(0, 0, 0, .12);
  color: #fff;
  position: relative;
  left: -100%;
  height: 100%;
  width: 200%;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
  display: flex;
}

.container.right-panel-active .overlay-container {
  transform: translateX(-100%);
}
.container.right-panel-active .overlay {
  transform: translateX(50%);
}

.overlay-panel {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  top: 0;
  height: 100%;
  width: 50%;
  transition: transform 0.6s ease-in-out, opacity 0.6s ease-in-out;
}

.overlay-content {
  background: rgba(255, 255, 255, 0.2);
  padding: 40px 30px 32px 30px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  border-radius: 10px;
  flex-direction: column;
  align-items: center;
  justify-content: center; /* 垂直居中 */
  text-align: center; /* 水平居中 */
  margin: 0 auto; /* 水平居中 */
  max-width: 300px; /* 限制宽度 */
  height: 200px; /* 固定高度以确保居中 */
}

.overlay-panel h2{
  font-size: 1.5rem;
}

.overlay-panel h2,
.overlay-panel p {
  color: #fff;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
}

.overlay-left {
  left: 0;
  transform: translateX(-20%);
}
.overlay-right {
  right: 0;
  transform: translateX(0);
}
.container.right-panel-active .overlay-left {
  transform: translateX(0);
}
.container.right-panel-active .overlay-right {
  transform: translateX(20%);
}

.title {
  font-weight: 700;
  font-size: 2.2rem;
  margin-bottom: 24px;
  color: #222;
}
.input {
  background: #f6f6f6;
  border: none;
  padding: 12px 16px;
  margin: 8px 0;
  width: 100%;
  border-radius: 4px;
  font-size: 16px;
}
.btn {
  border-radius: 20px;
  border: 1px solid #008CBA;
  background: linear-gradient(90deg, #008CBA 0%, #00CED1 74%);
  color: #fff;
  font-size: 16px;
  font-weight: bold;
  padding: 12px 45px;
  letter-spacing: 1px;
  text-transform: uppercase;
  margin-top: 20px;
  cursor: pointer;
  transition: transform 80ms ease-in;
}
.btn.ghost {
  background: transparent;
  border: 2px solid #fff;
  color: #fff;
}
.btn.ghost:hover {
  background: #fff;
  color: #0367a6;
}
.link {
  color: #0367a6;
  font-size: 0.9rem;
  margin: 10px 0;
  display: block;
}
</style>